File Storage Types
The storage type is a description of how the data is stored in the data file. Table 2 lists the definitions used during interactive bcp
and what appears in the format file. The storage type allows data to be
copied as its base type (native format), as implicitly converted
between types (tinyint to smallint), or as a string (in character or Unicode format).
Table 2. Storage Data Types
File Storage Type | Interactive Prompt | Host File Data Type |
---|
char | c[har] | SQLCHAR |
varchar | c[har] | SQLCHAR |
nchar | w | SQLNCHAR |
nvarchar | w | SQLNCHAR |
text | T[ext] | SQLCHAR |
ntext | W | SQLNCHAR |
binary | x | SQLBINARY |
varbinary | x | SQLBINARY |
image | I[mage] | SQLBINARY |
datetime | d[ate] | SQLDATETIME |
smalldatetime | D | SQLDATETIM4 |
decimal | n | SQLDECIMAL |
numeric | n | SQLNUMERIC |
float | f[loat] | SQLFLT8 |
real | r | SQLFLT4 |
int | i[nt] | SQLINT |
smallint | s[mallint] | SQLSMALLINT |
tinyint | t[inyint] | SQLTINYINT |
money | m[oney] | SQLMONEY |
smallmoney | M | SQLMONEY4 |
bit | b[it] | SQLBIT |
uniqueidentifier | u | SQLUNIQUEID |
timestamp | x | SQLBINARY |
Note
If the table makes use of
user-defined data types, these customized data types appear in the
format file as their base data type.
If you are having problems loading certain fields into your table, you can try the following tricks:
Copy the data in as char data types and force SQL Server to do the conversion for you.
Duplicate the table and replace all the SQL Server data types with char or varchar of a length sufficient to hold the value. This trick allows you to further manipulate the data with T-SQL after it is loaded.
Prefix Lengths
To maintain compactness in native data files, bcp
precedes each field with a prefix length that indicates the length of
the data stored. The space for storing this information is specified in
characters and is called the prefix length.
Table 3 indicates the value to specify for prefix length for each of the data types.
Table 3. Prefix Length Values
Prefix Length | Data Types to Use |
---|
0 | Non-null data of type bit or numerics (int, real,
and so on). Use this value when no prefix characters are wanted. This
value causes the field to be padded with spaces to the size indicated
for the field length. |
1 | Non-null data of type binary or varbinary or null data, with the exception of text, ntext, and image. Use this value for any data (except bit, binary, varbinary, text, ntext, and image) that you want stored using a character-based data type. |
2 | When storing the data types binary or varbinary as character-based data types, 2 bytes of char file storage and 4 bytes of nchar file storage are required for each byte of binary table data. |
4 | For the data types text, ntext, and image. |
Prefix lengths are likely to exist only within data files created using bcp. It is unlikely that you will encounter a reason to change the defaults bcp has chosen for you.
Field Lengths
When using either the native
or character data format, you must specify the maximum length of each
field. When converting data types to strings, bcp suggests lengths large enough to store the entire range of values for each particular data type. Table 4 lists the default values for each of the data formats.
Table 4. Default Field Lengths for Data Formats
Data Type | Length (/c) | Length (/n) |
---|
bit | 1 | 1 |
binary | Column length × 2 | Column length |
datetime | 24 | 8 |
smalldatetime | 24 | 4 |
float | 30 | 8 |
real | 30 | 4 |
int | 12 | 4 |
smallint | 7 | 2 |
tinyint | 5 | 1 |
money | 30 | 8 |
smallmoney | 30 | 4 |
decimal | 41 | up to 17 |
numeric | 41 | up to 17 |
uniqueidentifier | 37 | 16 |
Note
You must specify a field length that is long enough for the data being stored. bcp
error messages regarding overflows indicate that the data value has
been truncated in at least one of the fields. If the operation is a
load, an overflow error usually results in bcp terminating. However, if you are dumping the data to a file, the data is truncated without error messages.
The field length value is used only when the prefix length is 0 and you have specified no terminators. In essence, you are doing a fixed-length data copy. bcp uses exactly the amount of space stated by the field length for each field; unused space within the field is padded out.
Note
Preexisting spaces in the data are not distinguished from added padding.